﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SongSharp.Domain.Suggesting;
using SongSharp.Domain.Suggesting.VaryVogueValue;
using SongSharp.Domain.Objects;
using SongSharp.Logging;

namespace SongSharp.Appliance
{
    public static class SuggestionApp
    {
        public const int MAX_SUGGESTIONS = 200;
        public static SuggestionManager SuggestionManager { get; set; }

        static SuggestionApp()
        {
            SuggestionManager = SuggestionManager.CreateInstance(new Weight(8.0));
        }

        internal static void TrackPlayed(Track track)
        {
            if (track != null && !track.IsTransient)
            {
                track.Statistic.PerformEnvironmentSnapShot(TrackHandlingType.PlayedTrackToEnd);
                track.Statistic.PlayedExplicitCount++;
                track.SaveStatistic();
                ChanceValue v = SuggestionManager.GenerateVaryVogueValue(track);
            }
        }

        internal static void TrackSkipped(Track track)
        {
            if (track != null && !track.IsTransient)
            {
                track.Statistic.PerformEnvironmentSnapShot(TrackHandlingType.SkippedTrack);
                track.Statistic.SkippedCount++;
                track.SaveStatistic();
                ChanceValue v = SuggestionManager.GenerateVaryVogueValue(track);
            }
        }

        public static List<Track> CreateSuggestions()
        {
            return CreateSuggestions(Track.GetAll());
        }

        public static List<Track> CreateSuggestions(List<Track> tracks)
        {
            List<Track> list;

            foreach (Track t in tracks)
            {
                t.Statistic.VaryVogueValue = SuggestionManager.GenerateVaryVogueValue(t).Value * 
                    SuggestionManager.GenerateWeightedRandom();
            }

            list = (from t in tracks
                   orderby t.Statistic.VaryVogueValue descending
                   select t).ToList();

            if (list.Count > MAX_SUGGESTIONS)
                list = list.GetRange(0, MAX_SUGGESTIONS);

            return list;
        }
    }
}
